package com.plan.base.util;

import java.math.BigInteger;

public class VerificationCodeUtil {
    private static final char[] STUFFS = new char[]{'V', 'B', '5', 'D', 'E', 'F', '4', 'H', 'J', '8', 'L', 'M', 'N', 'P', 'Q', '2', 'S', 'T', 'U', 'A', '7', 'X', 'Y', 'Z', 'R', '3', 'C', '6', 'W', 'K', '9'};
    private static final int[] STUFFS2 = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

    public static String getInvitationCode(Long v) {
        String r31 = changeIntRadixFrom10To31(v.toString());
        StringBuilder builder = new StringBuilder(10);

        for (int i = 0; i < r31.length(); ++i) {
            builder.append(STUFFS[STUFFS2[r31.charAt(i)]]);
        }

        if (builder.length() < 6) {
            builder.append("GGGGGG".subSequence(0, 6 - builder.length()));
        }

        return builder.toString();
    }

    public static String generateVerificationCode() {
        int verificationCode = (int) ((Math.random() * 9.0 + 1.0) * 100000.0);
        return "" + verificationCode;
    }

    public static boolean isVerificationCode(String value) {
        return value != null && value.matches("[0-9]{6}");
    }


    public static String changeIntRadixFrom10To31(String num) {
        int f = 10;
        int t = 31;
        return (new BigInteger(num, f)).toString(t);
    }

}
